package mashibing.class13;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索二叉树-返回最大子节点头节点
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-22
 */
public class Class13_2_MaxSubBSTHead {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static class Info {
        public Node maxSubBSTHead;
        public int maxSubBSTSize;
        public int min;
        public int max;

        public Info(Node n, int b, int mi, int ma) {
            this.maxSubBSTHead = n;
            this.maxSubBSTSize = b;
            this.min = mi;
            this.max = ma;
        }
    }

    public static Node maxSubBSTHead1(Node node) {
        if (node == null) {
            return null;
        }
        return process(node).maxSubBSTHead;
    }

    public static Info process(Node node) {
        if (node == null) {
            return null;
        }
        Info leftInfo = process(node.left);
        Info rightInfo = process(node.right);
        Node maxSubBSTHead = null;
        int maxSubBSTSize = 0;
        int min = node.value;
        int max = node.value;
        if (leftInfo != null) {
            min = Math.min(min, leftInfo.min);
            max = Math.max(max, leftInfo.max);
            maxSubBSTHead = leftInfo.maxSubBSTHead;
            maxSubBSTSize = leftInfo.maxSubBSTSize;
        }
        if (rightInfo != null) {
            min = Math.min(min, rightInfo.min);
            max = Math.max(max, rightInfo.max);
            if (rightInfo.maxSubBSTSize > maxSubBSTSize) {
                maxSubBSTHead = rightInfo.maxSubBSTHead;
                maxSubBSTSize = rightInfo.maxSubBSTSize;
            }
        }
        // 是否为搜索二叉树（x得左节点等于左字数得最大头节点）
        if ((leftInfo == null || (node.left == leftInfo.maxSubBSTHead && node.value > leftInfo.max))
                && (rightInfo == null || (node.right == rightInfo.maxSubBSTHead && node.value < rightInfo.min))) {
            maxSubBSTHead = node;
            int leftMaxSubBSTSize = leftInfo == null ? 0 : leftInfo.maxSubBSTSize;
            int rightMaxSubBSTSize = rightInfo == null ? 0 : rightInfo.maxSubBSTSize;
            maxSubBSTSize = leftMaxSubBSTSize + rightMaxSubBSTSize + 1;
        }
        return new Info(maxSubBSTHead, maxSubBSTSize, min, max);
    }


    // 非递归写法（中序遍历）
    public static Node maxSubBSTHead2(Node node) {
        if(node == null){
            return null;
        }
        if(getBSTSize(node) != 0) {
            return node;
        }
        Node leftNode = maxSubBSTHead2(node.left);
        Node rightNode = maxSubBSTHead2(node.right);
        return getBSTSize(leftNode) >= getBSTSize(rightNode) ? leftNode : rightNode;
    }

    public static int getBSTSize(Node node){
        if(node == null){
            return 0;
        }
        List<Node> list = new ArrayList<>();
        in(node, list);
        for (int i = 1; i < list.size(); i++) {
            if(list.get(i).value <= list.get(i - 1).value){
                return 0;
            }
        }
        return list.size();
    }

    public static void in(Node node,List<Node> list){
        if(node == null){
            return;
        }
        in(node.left,list);
        list.add(node);
        in(node.right,list);
    }


    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (maxSubBSTHead1(head) != maxSubBSTHead2(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }

}
